﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Discovery;
using System.ServiceModel.Security;
using System.Text;
using System.Threading;
using System.Windows;
using VMDS.Business.Entities;
using VMDS.GUIClient.MainServiceReference;
using VMDS.GUIClient.ServiceReference;
using VMDS.Settings;


namespace VMDS.GUIClient.Model
{
    public class ClientCallback:IClientServiceContractCallback
    {
        public void OnTaskChanged(TaskDataPack task)
        {
          
        }

        public void OnTaskAdded(TaskDataPack task)
        {
            MessageBox.Show("added");
        }

        public void OnTaskCmpleted(TaskDataPack task)
        {
          
        }
    }

    public class ServiceLocator
    {
        //private static DiscoveryClient dc = new DiscoveryClient(new UdpDiscoveryEndpoint());
       // private static FindCriteria criteria = new FindCriteria(typeof(IVmdsService));
        static MainServiceContractClient MS;
        public static MainServiceContractClient MainService
        {
            get {
                if (MS==null) MS=new MainServiceContractClient();
                if (MS.State==CommunicationState.Faulted) MS=new MainServiceContractClient();
                return MS;
            } 
        }

        public static ClientCallback CallBack;
        public static ClientServiceContractClient ClientService
        {
            get
            {
                //return new ClientServiceContractClient(new NetTcpBinding(), new EndpointAddress(CsAddress));
                if (CallBack==null)CallBack=new ClientCallback();
                InstanceContext context=new InstanceContext(CallBack);
                var client= new ClientServiceContractClient(context, new NetTcpBinding(), new EndpointAddress(CsAddress));
               // try { client.Register(); }
              //  catch { }
                return client;
            }
        }
        public static bool Check()
        {
            try
            {
                if (MainService.State!=CommunicationState.Opened) MainService.Open();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static string MsAddress { get; private set; }
        public static string CsAddress { get; private set; }
     //   public static bool Completed { get; private set; }
       // public static int SearchProgress { get; private set; }


        /*public static void Find()
        {
           
           FindResponse fr = dc.Find(criteria);
            if (fr.Endpoints.Count > 0)
            {
                Completed = true;
                foreach (var endpoint in fr.Endpoints)
                {
                    try
                    {
                        EndpointAddress epaddr = endpoint.Address;
                        Binding binding = new NetTcpBinding();
                        VmdsServiceClient client = new VmdsServiceClient(binding, epaddr);
                        client.Open();
                        _client = client;
                        CurrentAddress = endpoint.Address.ToString();
                        break;
                    }
                    catch { }
                }
               
            }
        }*/

        static ServiceLocator()
        {
            var cfg = new ClientConfiguration("d:\\virtual\\data\\config.ini");
            MsAddress = cfg.MsAddress;
            CsAddress = cfg.CsAddress;
        }
        
    }
}
